package com.etc.layuisys.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.layuisys.entity.Users;
import com.etc.layuisys.service.UsersService;
import com.etc.layuisys.service.impl.UsersServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class UsersServlet
 */
@WebServlet("/users/list")
public class UsersServlet extends HttpServlet {

	// 创建service对象
	private UsersService us = new UsersServiceImpl();
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		response.setContentType("application/json");
		int page = 1;
		int limit = 10;
		String content = "";

		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		if (request.getParameter("limit") != null) {
			limit = Integer.parseInt(request.getParameter("limit"));
		}

		if (request.getParameter("content") != null) {
			content = request.getParameter("content");
		}
		PageData<Users> pd = us.getUsersByPage(page,limit, content);
		
		pd.setCode(0);
		
		Gson gson=new Gson();
		String str = gson.toJson(pd);
		out.print(str); // json? json对象
		out.close();



	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
